package custom.cards;

import java.io.Serializable;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

public class Card extends ImageView implements Serializable
{


	private static final long serialVersionUID = 1L;
	
 /**
  * the cards number
  */
	public final CardNumber number;
	/**
	 * the cards suite
	 */
	public final CardSuite Suite;
	 /**
	  *  the base card image
	 */
	public Drawable cardImage;
	/**
	 * Used to determine if clicked or just dragged
	 */
	public boolean actuallyDragged = false;
	public boolean clickedObect = false;
	boolean cardShowing;
	public static Drawable backImage;

	public Card(CardNumber num, CardSuite suite, Context context, boolean showFront)
	{
		super(context);
		this.number = num;
		this.Suite = suite;
		cardImage = context.getResources().getDrawable(getCardDrawableId());
		backImage = context.getResources().getDrawable(R.drawable.back);
		cardShowing = showFront;
		if(showFront)
		{
			this.setImageDrawable(cardImage);
		}
		else
		{
				
			this.setImageDrawable(backImage);
		}
	}
	
	/**
	 * changes the image being shown
	 */
	public void Swap()
	{
		if(cardShowing)
		{
			cardShowing = false;
			this.setImageDrawable(backImage);
		}
		else
		{
			cardShowing = true;
			this.setImageDrawable(cardImage);
		}
	}
	
	public Card(String toStringVersion, Context context, boolean showFront)
	{
		super(context);
		String[] split = toStringVersion.split(" ");
		
		if(split.length == 4)
		{
			String number = split[1];
			String suit = split[3];
			
			this.number = CardNumber.values()[Integer.parseInt(number)];
			this.Suite = CardSuite.values()[Integer.parseInt(suit)];
			
			cardImage = context.getResources().getDrawable(getCardDrawableId());
			backImage = context.getResources().getDrawable(R.drawable.back);
			cardShowing = showFront;
			if(showFront)
			{
				this.setImageDrawable(cardImage);
			}	
			else
			{				
				this.setImageDrawable(backImage);
			}
			

		}
		else
		{
			this.number = null;
			this.Suite = null;

		}
		
		
		
		
		
	}
	
	 public boolean equals(Object obj) 
	 {
	        if (obj == null)
	            return false;
	        if (obj == this)
	            return true;
	        if (obj.getClass() != getClass())
	            return false;

	        if(this == null && obj != null)
	        {
	        	return false;
	        }
	        
	        Card c = (Card) obj;
	        
	        if(c.number == this.number && c.Suite == this.Suite)
	        {
	        	return true;
	        }
	        else
	        {
	        	return false;
	        }
	    }
	
	public String toNumberString()
	{
		
		return String.format("The %d of %d", this.number.ordinal(), this.Suite.ordinal());
	}

	public Integer getCardDrawableId()
	{
		switch(this.number)
		{
		case Two:
			switch(this.Suite)
			{
			case Hearts:
				return R.drawable.h2;
			case Clubs: 
				return R.drawable.c2;
			case Spades:
				return R.drawable.s2;
			case Diamonds:
				return R.drawable.d2;
			}
		case Three:
			switch(this.Suite)
			{
			case Hearts:
				return R.drawable.h3;
			case Clubs: 
				return R.drawable.c3;
			case Spades:
				return R.drawable.s3;
			case Diamonds:
				return R.drawable.d3;
			}
		case Four:
			switch(this.Suite)
			{
			case Hearts:
				return R.drawable.h4;
			case Clubs: 
				return R.drawable.c4;
			case Spades:
				return R.drawable.s4;
			case Diamonds:
				return R.drawable.d4;
			}
		case Five:
			switch(this.Suite)
			{
			case Hearts:
				return R.drawable.h5;
			case Clubs: 
				return R.drawable.c5;
			case Spades:
				return R.drawable.s5;
			case Diamonds:
				return R.drawable.d5;
			}
		case Six:
			switch(this.Suite)
			{
			case Hearts:
				return R.drawable.h6;
			case Clubs: 
				return R.drawable.c6;
			case Spades:
				return R.drawable.s6;
			case Diamonds:
				return R.drawable.d6;
			}
		case Seven:
			switch(this.Suite)
			{
			case Hearts:
				return R.drawable.h7;
			case Clubs: 
				return R.drawable.c7;
			case Spades:
				return R.drawable.s7;
			case Diamonds:
				return R.drawable.d7;
			}
		case Eight:
			switch(this.Suite)
			{
			case Hearts:
				return R.drawable.h8;
			case Clubs: 
				return R.drawable.c8;
			case Spades:
				return R.drawable.s8;
			case Diamonds:
				return R.drawable.d8;
			}
		case Nine:
			switch(this.Suite)
			{
			case Hearts:
				return R.drawable.h9;
			case Clubs: 
				return R.drawable.c9;
			case Spades:
				return R.drawable.s9;
			case Diamonds:
				return R.drawable.d9;
			}
		case Ten:
			switch(this.Suite)
			{
			case Hearts:
				return R.drawable.h10;
			case Clubs: 
				return R.drawable.c10;
			case Spades:
				return R.drawable.s10;
			case Diamonds:
				return R.drawable.d10;
			}
		case Jack:
			switch(this.Suite)
			{
			case Hearts:
				return R.drawable.hj;
			case Clubs: 
				return R.drawable.cj;
			case Spades:
				return R.drawable.sj;
			case Diamonds:
				return R.drawable.dj;
			}
		case Queen:
			switch(this.Suite)
			{
			case Hearts:
				return R.drawable.hq;
			case Clubs: 
				return R.drawable.cq;
			case Spades:
				return R.drawable.sq;
			case Diamonds:
				return R.drawable.dq;
			}
		case King:
			switch(this.Suite)
			{
			case Hearts:
				return R.drawable.hk;
			case Clubs: 
				return R.drawable.ck;
			case Spades:
				return R.drawable.sk;
			case Diamonds:
				return R.drawable.dk;
			}
		case Ace:
			switch(this.Suite)
			{
			case Hearts:
				return R.drawable.ah;
			case Clubs: 
				return R.drawable.ac;
			case Spades:
				return R.drawable.as;
			case Diamonds:
				return R.drawable.ad;
			}
		case Joker:
			switch(this.Suite)
			{
			case Hearts:
			case Diamonds:
				return R.drawable.blackjoker;
			case Spades:
			case Clubs:
				return R.drawable.redjoker;
			}
		}
		return -1;
	}

	@Override 
	public String toString()
	{
			return String.format("The %s of %s", this.number, this.Suite);
	}

	public enum CardSuite
	{
		Hearts,
		Clubs,
		Spades,
		Diamonds	
	}
	
	public enum CardNumber
	{
		Two,
		Three,
		Four,
		Five,
		Six,
		Seven,
		Eight,
		Nine,
		Ten,
		Jack,
		Queen,
		King,
		Ace,
		Joker
	}
}

